package com.orangelife.mobile.annoucement.biz;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.google.gson.reflect.TypeToken;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.annoucement.activity.AnnoucementListActivity;
import com.orangelife.mobile.annoucement.bean.AnnoucementBean;
import com.orangelife.mobile.constants.AnnouncementURL;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息
 * Created by yyx on 2015/11/10.Modify by wgf on 160701.
 *
 * @author wgf
 */
public class MessageHttpBiz {

    /**
     * 请求消息列表
     *
     * @param type     1=系统消息，2=公告消息；
     * @param pageNo
     * @param pageSize
     * @param callback
     */
    public static void getList(String type, int pageNo, int pageSize, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, String> map = new HashMap<>();
        String url;
        if (AnnoucementListActivity.TYPE_MESSAGE.equals(type)) {
            url = AnnouncementURL.URL_MESSAGE_LIST;
        } else {
            map.put("{type}", "2");/**/
            url = AnnouncementURL.ANNOUNCEMENT_LIST;
        }
        map.put("pageSize", String.valueOf(pageSize));
        map.put("pageIndex", String.valueOf(pageNo));
        VolleyHttpUtil.getInstance().getWithCallback(url, map, callback);
    }

    /**
     * 请求详情
     *
     * @param callback
     */
    public static void requestDetail(String annoID, String type, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("{annoID}", annoID);
        map.put("type", type);
        VolleyHttpUtil.getInstance().getWithCallback(AnnouncementURL.URL_ANNOUNCEMENT_DETAIL, map, callback);
    }

    /**
     * 列表回调
     *
     * @param response
     * @return
     */
    public static List<Map<String, Object>> handleList(JSONObject response) {
        List<Map<String, Object>> list = null;
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            list = OrangeDataHandler.getListFromJsonWithPageEntity(
                    response, new TypeToken<List<Map<String, Object>>>() {
                    }.getType(), null, null);

        }
        return list;
    }

    /**
     * 详情回调
     *
     * @param response
     * @return
     */
    public static String handleOrderDetail(JSONObject response) {
        String url = null;
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            AnnoucementBean bean = (AnnoucementBean) OrangeDataHandler.getBeanWithEntity(response, AnnoucementBean.class);
            if (bean != null) {
                return bean.getUrl();
            }
        }
        return url;
    }

    /**
     * 更新商家信息列表
     *
     * @param list2
     */
    // FIXME: 2016/7/5 删除这玩意，优化代码
    public void updateListView(List<Map<String, Object>> list2, int flags, PullToRefreshListView lv,
                               List<Map<String, Object>> list, FatherAdapter adapter, int type) {
        if (list2 == null) {
            if (type == 2) {
                lv.setFootHintText("暂无公告");
            }
            if (flags == 1) {
                list.clear();
                lv.setPullLoadEnable(false);
                adapter.setList(list);
                adapter.notifyDataSetChanged();
            } else {
                lv.setPullLoadEnable(false);
            }
        }
        if (list2 != null && list != null) {
            if (list2.size() == 0) {
                lv.setPullLoadEnable(false);
                if (type == 2) {
                    lv.setFootHintText("暂无公告");
                }
            } else {
                if (flags == 1) {
                    list.clear();
                    lv.setPullLoadEnable(true);
                    list.addAll(list2);
                    adapter.setList(list);
                    adapter.notifyDataSetChanged();
                } else {
                    lv.setPullLoadEnable(true);
                    list.addAll(list2);
                    adapter.setList(list);
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }
}
